﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
namespace RC1.admin
{
    public partial class _class : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                populatePage();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            addSubjectGroup();
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var results = (from s in db.subjects
                           select s);
            subjectDropDownList.DataSource = db.subjects;
            subjectDropDownList.DataTextField = "subject_name";
            subjectDropDownList.DataValueField = "subject_id";
            subjectDropDownList.DataBind();

            var teacherResults = (from u in db.lpusers
                                  join ui in db.user_info on u.user_id equals ui.user_id
                                  where u.user_level == 1
                                  select new 
                                    { 
                                        user_id = u.user_id,
                                        name = ui.user_info_first_name + " " + ui.user_info_last_name
                                    });
            teacherDropDownList.DataSource = teacherResults;
            teacherDropDownList.DataTextField = "name";
            teacherDropDownList.DataValueField = "user_id";
            teacherDropDownList.DataBind();
        }

        private void addSubjectGroup()
        {
            String className = classNameTextBox.Text;
            String description = descriptionTextBox.Text;
            int subjectID = Convert.ToInt32(subjectDropDownList.SelectedValue);
            int teacherID = Convert.ToInt32(teacherDropDownList.SelectedValue);

            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            @class cl = new @class();
            cl.class_description = description;
            cl.class_name = className;
            cl.subject_id = subjectID;
            cl.user_id = teacherID;
            db.classes.AddObject(cl);
            db.SaveChanges();
            Server.Transfer("class.aspx");
        }

        protected String getButtons(int class_id)
        {
            String temp = "<a href='edit_class.aspx?id=" + class_id + "'><img src='../images/edit-icon.png' alt='Broyt' width='20' height='20'/></a>";
            return temp;
        }

        protected void GridviewCommandEventHandler(Object sender, GridViewCommandEventArgs args)
        {
            if (args.CommandName.Equals("showClass"))
            {
                String id = GridView1.DataKeys[Convert.ToInt32(args.CommandArgument)].Value.ToString();
                Session["class_id"] = id;
                Server.Transfer("populateClass.aspx");
            }
        }
    }
}